package com.xique.door.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.door.bean.domain.AccessDevice;
import com.xique.door.bean.domain.AccessDeviceV1;
import com.xique.door.bean.domain.User;
import com.xique.door.bean.request.AccessDevice.HouseSearchDeviceRequest;
import com.xique.door.bean.search.AccessDeviceListSearch;
import com.xique.door.bean.search.device.DeviceListSearch;
import com.xique.door.bean.vo.ParkInfoIssueVO;
import com.xique.door.constant.AccessDeviceConstant;
import com.xique.door.constant.DoorDeviceConstant;
import com.xique.door.mapper.AccessDeviceMapper;
import com.xique.door.mapper.AccessDeviceMapperV1;
import com.xique.door.service.IAccessDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 门禁设备Service业务层处理
 *
 * @author ww
 * @date 2021-06-10
 */
@Service
public class AccessDeviceServiceImpl implements IAccessDeviceService {
    @Autowired
    private AccessDeviceMapper accessDeviceMapper;

    @Autowired
    private AccessDeviceMapperV1 accessDeviceMapperV1;


    @Override
    public List<ParkInfoIssueVO> selectIssueAccessDevice(String districtName) {
        return accessDeviceMapper.selectIssueAccessDevice(districtName);
    }

    @Override
    public List<ParkInfoIssueVO> selectIssuedAccessDevice(List<Long> accessDeviceids) {
        return accessDeviceMapper.selectIssuedAccessDevice(accessDeviceids);
    }

    /**
     * 查询门禁设备
     *
     * @param id 门禁设备ID
     * @return 门禁设备
     */
    @Override
    public AccessDevice selectAccessDeviceById(Long id) {
        return accessDeviceMapper.selectAccessDeviceById(id);
    }

    /**
     * 查询门禁设备
     *
     * @param ids 门禁设备ID
     * @return 门禁设备
     */
    @Override
    public List<AccessDevice> selectAccessDeviceByIds(List<Long> ids) {
        return this.selectAccessDeviceByIds(ids, null);
    }

    /**
     * 查询门禁设备
     *
     * @param ids 门禁设备ID
     * @return 门禁设备
     */
    @Override
    public List<AccessDevice> selectAccessDeviceListIds(List<Long> ids, AccessDevice accessDevice) {
        return accessDeviceMapper.selectAccessDeviceListIds(ids, accessDevice);
    }


    @Override
    public List<AccessDevice> selectAccessDeviceWithBuildByIds(List<Long> ids) {
        return accessDeviceMapper.selectAccessDeviceWithBuildByIds(ids);
    }

    @Override
    public List<AccessDevice> selectAccessDeviceByIds(List<Long> ids, String status) {
        if (ids.isEmpty()) {
            return new ArrayList<>();
        }
        return accessDeviceMapper.selectAccessDeviceByIds(ids, status);
    }

    /**
     * 查询门禁设备列表
     *
     * @param accessDevice 门禁设备
     * @return 门禁设备
     */
    @Override
    public List<AccessDevice> selectAccessDeviceList(AccessDevice accessDevice) {
        return accessDeviceMapper.selectAccessDeviceList(accessDevice);
    }

    /**
     * 新增门禁设备
     *
     * @param accessDevice 门禁设备
     * @return 结果
     */
    @Override
    public int insertAccessDevice(AccessDevice accessDevice) {
        accessDevice.setCreateTime(DateUtils.getNowDate());
        accessDevice.setUpdateTime(accessDevice.getCreateTime());
        accessDevice.setCreateUser(SecurityUtils.getUserId());
        accessDevice.setUpdateUser(accessDevice.getCreateUser());
        return accessDeviceMapper.insertAccessDevice(accessDevice);
    }

    /**
     * 修改门禁设备
     *
     * @param accessDevice 门禁设备
     * @return 结果
     */
    @Override
    public int updateAccessDevice(AccessDevice accessDevice) {
        accessDevice.setUpdateTime(DateUtils.getNowDate());
        accessDevice.setUpdateUser(SecurityUtils.getUserId());
        return accessDeviceMapper.updateAccessDevice(accessDevice);
    }

    /**
     * 删除门禁设备对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAccessDeviceByIds(List<Long> ids) {
        if (ids.isEmpty()) {
            return 1;
        }
        return accessDeviceMapper.deleteAccessDeviceByIds(ids);
    }

    @Override
    public List<AccessDevice> selectAccessDeviceListByUser(User user) {
        return accessDeviceMapper.selectAccessDeviceListByUser(user);
    }


    /**
     * 查询门禁设备列表
     *
     * @param search 门禁设备
     * @return 门禁设备
     */
    @Override
    public List<AccessDeviceV1> selectAccessDeviceList(AccessDeviceListSearch search) {
        QueryWrapper<AccessDeviceV1> wrapper = new QueryWrapper<>();
        if (CollectionUtils.isNotEmpty(search.getDeviceIds())) {
            wrapper.in("id", search.getDeviceIds());
        }
        if (StringUtils.isNotNull(search.getParkId())) {
            wrapper.eq("park_id", search.getParkId());
        }
        if (CollectionUtils.isNotEmpty(search.getForeignIds())) {
            wrapper.in("foreign_id", search.getForeignIds());
        }
        if (StringUtils.isNotNull(search.getForeignId())) {
            wrapper.eq("foreign_id", search.getForeignId());
        }
        return accessDeviceMapperV1.selectList(wrapper);
    }


    /**
     * 批量删除门禁设备
     *
     * @param ids 需要删除的门禁设备ID
     * @return 结果
     */
    @Override
    public int deleteAccessDeviceByIds(Long[] ids) {
        return accessDeviceMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除门禁设备信息
     *
     * @param id 门禁设备ID
     * @return 结果
     */
    @Override
    public int deleteAccessDeviceById(Long id) {
        return accessDeviceMapper.deleteAccessDeviceById(id);
    }

    @Override
    public AccessDevice selectAccessDeviceBySn(String sn) {
        return accessDeviceMapper.selectAccessDeviceBySn(sn);
    }

    @Override
    public List<AccessDevice> selectAccessDevicesByHouseIds(HouseSearchDeviceRequest request) {
        return accessDeviceMapper.selectAccessDeviceByHouseIds(request);
    }

    @Override
    public IPage<AccessDeviceV1> selectDeviceListV1(DeviceListSearch search) {

        QueryWrapper<AccessDeviceV1> condition = new QueryWrapper<>();
        if (StringUtils.isNotNull(search.getParkId())) {
            condition.eq("park_id", search.getParkId());
        }

        Page<AccessDeviceV1> page = new Page<>();
        if (StringUtils.isNotNull(search.getPageNum()) && StringUtils.isNotNull(search.getPageSize())) {
            page.setCurrent(search.getPageNum());
            page.setSize(search.getPageSize());
        }

        condition.orderByDesc("create_time");

        return accessDeviceMapperV1.selectPage(page, condition);
    }

    @Override
    public List<AccessDeviceV1> selectFreeviewDeviceList(DeviceListSearch search) {
        QueryWrapper<AccessDeviceV1> condition = new QueryWrapper<>();
        if (StringUtils.isNotNull(search.getParkId())) {
            condition.eq("park_id", search.getParkId());
        }
        condition.in("model", Arrays.asList(DoorDeviceConstant.DeviceModel.QST_32, DoorDeviceConstant.DeviceModel.QST_45))
                .eq("status", AccessDeviceConstant.DeviceStatus.NORMAL);
        return accessDeviceMapperV1.selectList(condition);
    }

    public List<AccessDeviceV1> selecetAccessDevice(AccessDeviceV1 accessDeviceV1) {
        QueryWrapper<AccessDeviceV1> condition = new QueryWrapper<>();
        if (StringUtils.isNotNull(accessDeviceV1.getParkId())) {
            condition.eq("park_id", accessDeviceV1.getParkId());
        }
        if (StringUtils.isNotNull(accessDeviceV1.getAccessType())) {
            condition.eq("access_type", accessDeviceV1.getAccessType());
        }
        if (StringUtils.isNotNull(accessDeviceV1.getForeignId())) {
            condition.eq("foreign_id", accessDeviceV1.getForeignId());
        }
        condition.eq("status", "normal");
        return accessDeviceMapperV1.selectList(condition);
    }

}
